class CommentariesController < ApplicationController
  # GET /commentaries
  # GET /commentaries.json
  def index
    @commentaries = Commentary.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @commentaries }
    end
  end

  # GET /commentaries/1
  # GET /commentaries/1.json
  def show
    @commentary = Commentary.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @commentary }
    end
  end

  # GET /commentaries/new
  # GET /commentaries/new.json
  def new
    @commentary = Commentary.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @commentary }
    end
  end

  # GET /commentaries/1/edit
  def edit
    @commentary = Commentary.find(params[:id])
  end

  # POST /commentaries
  # POST /commentaries.json
  def create
    @commentary = Commentary.new(params[:commentary])

    respond_to do |format|
      if @commentary.save
        format.html { redirect_to(@commentary, :notice => 'Commentary was successfully created.') }
        format.json  { render :json => @commentary, :status => :created, :location => @commentary }
      else
        format.html { render :action => "new" }
        format.json  { render :json => @commentary.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /commentaries/1
  # PUT /commentaries/1.json
  def update
    @commentary = Commentary.find(params[:id])

    respond_to do |format|
      if @commentary.update_attributes(params[:commentary])
        format.html { redirect_to(@commentary, :notice => 'Commentary was successfully updated.') }
        format.json  { head :ok }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @commentary.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /commentaries/1
  # DELETE /commentaries/1.json
  def destroy
    @commentary = Commentary.find(params[:id])
    @commentary.destroy

    respond_to do |format|
      format.html { redirect_to(commentaries_url) }
      format.json  { head :ok }
    end
  end

  def getCommByRestid
    @comments = Commentary.where(:restaurant_id => params[:restid], :status => 2)
    respond_to do |format|
      format.json  { render :json => @comments }
    end
  end

  def updateCommentary
    @res = Reservation.find(params[:resid])
    @res.status = 3
    @res.save

    #cambio el comentario
    @com = @res.Commentary
    @com.cmt = params[:cmt]
    @com.rate = params[:rate]
    @com.rest_rate = params[:rrate]
    @com.local_rate = params[:lrate]
    @com.status = 2
    @com.save

    #hago el update de los rates del restaurante

    @commrest = Commentary.where(:restaurant_id => @com.restaurant_id)
    @rest = Restaurant.find(@com.restaurant_id)
    @rest.score = ( @rest.score * (@commrest.length  - 1 ) + @com.rest_rate ) / (@commrest.length)
    #(  @rest.score*(@commrest.length  -1) + @com.rest_rate) / @commrest.size
    @rest.save

    #hago el update de los rates del local

    @commlocal = Commentary.where(:local_id => @com.local_id)
    @local = Local.find(@com.local_id)
    @local.rate = ( @local.rate * (@commlocal.length  - 1 ) + @com.local_rate ) / (@commlocal.length)
    #(  @rest.score*(@commrest.length  -1) + @com.rest_rate) / @commrest.size
    @local.save


    respond_to do |format|
      format.json  { render :json => @com }
    end
  end


end
